﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Reflection;

namespace DevelopStudio.Basic
{
    public class AppEnum
    {
        public AppEnum()
        {
        }

        public static SortedList GetStatus(System.Type t)
        {
            SortedList list = new SortedList();

            Array a = Enum.GetValues(t);
            for (int i = 0; i < a.Length; i++)
            {
                string enumName = a.GetValue(i).ToString();
                int enumKey = (int)System.Enum.Parse(t, enumName);
                string enumDescription = GetDescription(t, enumKey);
                list.Add(enumKey, enumDescription);
            }
            return list;
        }

        /// <summary>
        /// 返回指定枚举类型的指定值的描述
        /// </summary>
        /// <param name="t">枚举类型</param>
        /// <param name="v">枚举值</param>
        /// <returns></returns>
        public static string GetDescription(System.Type t, object v)
        {
            try
            {
                FieldInfo fi = t.GetField(GetName(t, v));
                DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
                return (attributes.Length > 0) ? attributes[0].Description : GetName(t, v);
            }
            catch (Exception ex)
            {
                return "UNKNOWN";
            }
        }

        private static string GetName(System.Type t, object v)
        {
            try
            {
                return Enum.GetName(t, v);
            }
            catch
            {
                return "UNKNOWN";
            }
        }

        /// <summary>
        /// 获取枚举类型的key值
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        private static SortedList GetEnumKey(System.Type t)
        {
            SortedList list = new SortedList();

            Array a = Enum.GetValues(t);
            for (int i = 0; i < a.Length; i++)
            {
                string enumName = a.GetValue(i).ToString();
                int enumKey = (int)System.Enum.Parse(t, enumName);
                //string enumDescription = GetDescription(t, enumKey);
                list.Add(enumKey, enumKey);
            }
            return list;
        }

        /// <summary>
        /// 获取枚举类型的key值
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        private static object GetEnumKeyByDescription(System.Type t, string desc)
        {
            Array a = Enum.GetValues(t);
            for (int i = 0; i < a.Length; i++)
            {
                string description = GetDescription(t, a.GetValue(i));

                if (description.Equals(desc, StringComparison.OrdinalIgnoreCase) == true)
                {
                    return i;
                }
            }

            throw new InvalidEnumArgumentException(Properties.Resources.ErrorEnumDescription);
        }

        public enum BugStatus : int
        {
            [Description("未知")]
            None = 0,
            [Description("新建")]
            Created = 1,
            [Description("开发中")]
            Fixing = 2,
            [Description("已解决")]
            Resolved = 3,
            [Description("已关闭")]
            Closed = 5,
            [Description("取消/拒绝")]
            CancelOrReject = 6,
            [Description("重新打开")]
            Reopen = 7,
            [Description("挂起")]
            Suspended=8,
        }

        public static SortedList GetBugStatus()
        {
            return GetStatus(typeof(BugStatus));
        }

        public static BugStatus GetBugStatusByDescription(string description)
        {
            int value = (int)GetEnumKeyByDescription(typeof(BugStatus), description);

            return (BugStatus)value;
        }

        /// <summary>
        /// Code Review 状态
        /// </summary>
        public enum BugCodeReviewStatus
        {
            [Description("无审核")]
            None = 0,
            [Description("审核中")]
            Reviewing,
            [Description("拒绝")]
            Rejected,
            [Description("通过")]
            Approved
        }

        public static SortedList GetCodeReviewStatus()
        {
            return GetStatus(typeof(BugCodeReviewStatus));
        }

        public static BugCodeReviewStatus GetCodeReviewStatusByDescription(string description)
        {
            int value = (int)GetEnumKeyByDescription(typeof(BugCodeReviewStatus), description);

            return (BugCodeReviewStatus)value;
        }

        /// <summary>
        /// Bug类型
        /// </summary>
        public enum BugType
        {
            [Description("未知")]
            None = 0,
            [Description("缺陷")]
            Defect,
            [Description("功能")]
            Task,
            [Description("需求")]
            Requirement,
            [Description("运维事故")]
            OperationAccident,
            [Description("评审")]
            Review,
            [Description("运维缺陷")]
            OperationDefect = 7
        }

        public static SortedList GetBugType()
        {
            return GetStatus(typeof(BugType));
        }

        public static BugType GetBugTypeByDescription(string description)
        {
            int value = (int)GetEnumKeyByDescription(typeof(BugType), description);

            return (BugType)value;
        }

        public enum BugPriority
        {
            [Description("P4:低")]
            P4 = 3,
            [Description("P3:普通")]
            P3,
            [Description("P2:高")]
            P2,
            [Description("P1:紧急")]
            P1,
            [Description("P0:立刻")]
            P0
        }

        public static SortedList GetBugPriority()
        {
            return GetStatus(typeof(BugPriority));
        }

        public static BugPriority GetBugPriorityByDescription(string description)
        {
            int value = (int)GetEnumKeyByDescription(typeof(BugPriority), description);

            return (BugPriority)value;
        }

        public enum BugAssignedStatus
        {
            [Description("以前分配")]
            Added = 0,
            [Description("新分配")]
            NewAdded,
            [Description("解除分配")]
            Removed,
        }

        public enum ChangelistStatus
        {
            [Description("本地")]
            Local = 0,
            [Description("无效")]
            Disabled,
            [Description("发送审核中")]
            SendReviewing,
            [Description("审核拒绝")]
            Rejected,
            [Description("审核通过")]
            Approved,
            [Description("带提示审核通过")]
            ApprovedWithComment,
            [Description("取消审核")]
            CanceledReview,
            [Description("接受审核中")]
            ReceiveReviewing,
            [Description("提交后等待")]
            CommitedButWaiting,
            [Description("完成提交")]
            Commited
        }

        public enum ChangelistErrorCode
        {
            [Description("无错误")]
            None = 0,
            [Description("绑定Bug")]
            BugAssigned,
            [Description("代码评审")]
            CodeReview
        }

        public enum ActionStatus
        {
            [Description("无状态")]
            None = 0,
            [Description("成功")]
            Success,
            [Description("失败")]
            Failed,
            [Description("警告")]
            Warning,
        }

        public enum ContentKind
        {
            [Description("总类")]
            Group = 0,
            [Description("总结")]
            Summary,
            [Description("小项")]
            Item,
        }

        public enum WizardStep
        {
            [Description("无效")]
            Invalid = 0,
            [Description("自定义")]
            Custom,
            [Description("确认")]
            Confirm,
            [Description("处理")]
            Progress,
            [Description("完成")]
            Completion,
        }

        public enum IgnoredType
        {
            Directory,
            File,
            Extention
        }
    }
}
